/**
 * 
 */
package Exercise;

/**
 * @author zhang 找出数组中 连续和为 N 的最长序列的长度
 */
public class ArraySumN {

	private static int getLength(int[] array, int N) {
		int[] temp = new int[array.length];
		temp[0] = array[0];
		for (int i = 1; i < array.length; i++) {
			temp[i] = temp[i - 1] + array[i];
		}
		for (int i = temp.length - 1; i >= 0; --i) {
			array[i] = -1;
			for (int j = 0; j < i; j++) {
				if (temp[i] - temp[j] == N) {
					array[i] = i - j;
					break;
				}
			}
		}
		int max = -1;
		for (int i = 0; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
			}
		}
		return max;
	}

	public static void main(String[] args) {

		int[] array = { 1, 7, -1, 8, 2, 4, -3, -7, -6, 9, 1, 10, 12 };
		System.out.println(getLength(array, 16));
	}

}
